<!DOCTYPE html>
<html lang="zh-hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/wow/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/wow/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/wow/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/wow/images/logo.svg" color="#222">

<link rel="stylesheet" href="/wow/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"single-flower.gitee.io","root":"/wow/","images":"/wow/images","scheme":"Muse","version":"8.3.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"بحث...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="Vue路由钩子一、路由钩子函数路由钩子作用就是拦截导航栏，就是在跳转的时候进行判断vue-router 提供的导航钩子主要用来拦截导航，让它完成跳转或取消路由钩子分为三类： 全局的、单个路由独享的、或者组件级 二、全局钩子主要包括beforeEach和aftrEach,这类钩子主要作用于全局,一般用来判断权限,以及以及页面丢失时候需要执行的操作。 12345678beforeEach函数有三个参数">
<meta property="og:type" content="article">
<meta property="og:title" content="朵朵很哇塞">
<meta property="og:url" content="https://single-flower.gitee.io/wow/2021/01/28/zy/vue%E8%B7%AF%E7%94%B1%E9%92%A9%E5%AD%90/index.html">
<meta property="og:site_name" content="朵朵最可爱">
<meta property="og:description" content="Vue路由钩子一、路由钩子函数路由钩子作用就是拦截导航栏，就是在跳转的时候进行判断vue-router 提供的导航钩子主要用来拦截导航，让它完成跳转或取消路由钩子分为三类： 全局的、单个路由独享的、或者组件级 二、全局钩子主要包括beforeEach和aftrEach,这类钩子主要作用于全局,一般用来判断权限,以及以及页面丢失时候需要执行的操作。 12345678beforeEach函数有三个参数">
<meta property="og:locale">
<meta property="article:published_time" content="2021-01-28T07:07:03.446Z">
<meta property="article:modified_time" content="2021-04-13T07:05:49.230Z">
<meta property="article:author" content="X y">
<meta property="article:tag" content="-GIT">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://single-flower.gitee.io/wow/2021/01/28/zy/vue%E8%B7%AF%E7%94%B1%E9%92%A9%E5%AD%90/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-hans'
  };
</script>
<title>朵朵很哇塞 | 朵朵最可爱</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="تشغيل شريط التصفح" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/wow/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">朵朵最可爱</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/wow/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-about"><a href="/wow/about/" rel="section"><i class="fa fa-user fa-fw"></i>معلومات</a></li>
        <li class="menu-item menu-item-tags"><a href="/wow/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>الوسوم</a></li>
        <li class="menu-item menu-item-categories"><a href="/wow/categories/" rel="section"><i class="fa fa-th fa-fw"></i>التصنيفات</a></li>
        <li class="menu-item menu-item-archives"><a href="/wow/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>الأرشيفات</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          المحتويات
        </li>
        <li class="sidebar-nav-overview">
          عام
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Vue%E8%B7%AF%E7%94%B1%E9%92%A9%E5%AD%90"><span class="nav-number">1.</span> <span class="nav-text">Vue路由钩子</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E8%B7%AF%E7%94%B1%E9%92%A9%E5%AD%90%E5%87%BD%E6%95%B0"><span class="nav-number">1.1.</span> <span class="nav-text">一、路由钩子函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%85%A8%E5%B1%80%E9%92%A9%E5%AD%90"><span class="nav-number">1.2.</span> <span class="nav-text">二、全局钩子</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E5%8D%95%E4%B8%AA%E8%B7%AF%E7%94%B1%E9%92%A9%E5%AD%90"><span class="nav-number">1.3.</span> <span class="nav-text">三、单个路由钩子</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E7%BB%84%E4%BB%B6%E5%86%85%E7%9A%84%E9%92%A9%E5%AD%90"><span class="nav-number">1.4.</span> <span class="nav-text">四、组件内的钩子</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">X y</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/wow/archives/">
        
          <span class="site-state-item-count">23</span>
          <span class="site-state-item-name">المقالات</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">التصنيفات</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">الوسوم</span>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-hans">
    <link itemprop="mainEntityOfPage" href="https://single-flower.gitee.io/wow/2021/01/28/zy/vue%E8%B7%AF%E7%94%B1%E9%92%A9%E5%AD%90/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wow/images/avatar.gif">
      <meta itemprop="name" content="X y">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="朵朵最可爱">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          朵朵很哇塞
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">نُشر في</span>

      <time title="أُنشأ: 2021-01-28 15:07:03" itemprop="dateCreated datePublished" datetime="2021-01-28T15:07:03+08:00">2021-01-28</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">عُدل في</span>
        <time title="عُدل: 2021-04-13 15:05:49" itemprop="dateModified" datetime="2021-04-13T15:05:49+08:00">2021-04-13</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">في</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/wow/categories/GIT/" itemprop="url" rel="index"><span itemprop="name">-GIT</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="Vue路由钩子"><a href="#Vue路由钩子" class="headerlink" title="Vue路由钩子"></a>Vue路由钩子</h1><h2 id="一、路由钩子函数"><a href="#一、路由钩子函数" class="headerlink" title="一、路由钩子函数"></a>一、路由钩子函数</h2><p>路由钩子作用就是拦截导航栏，就是在跳转的时候进行判断<br>vue-router 提供的导航钩子主要用来拦截导航，让它完成跳转或取消<br>路由钩子分为三类： 全局的、单个路由独享的、或者组件级</p>
<h2 id="二、全局钩子"><a href="#二、全局钩子" class="headerlink" title="二、全局钩子"></a>二、全局钩子</h2><p>主要包括beforeEach和aftrEach,这类钩子主要作用于全局,一般用来判断权限,以及以及页面丢失时候需要执行的操作。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">beforeEach函数有三个参数：</span><br><span class="line">- to:router即将进入的路由对象</span><br><span class="line">- <span class="keyword">from</span>:当前导航即将离开的路由</span><br><span class="line">- next:<span class="built_in">Function</span>,进行管道中的一个钩子，如果执行完了，则导航的状态就是 confirmed （确认的）；否则为<span class="literal">false</span>，终止导航。</span><br><span class="line">可以使用router.beforeEach注册一个全局的 before 钩子</span><br><span class="line">router.beforeEach(<span class="function">(<span class="params">to, <span class="keyword">from</span>, next</span>) =&gt;</span> &#123; <span class="comment">// ... &#125;)</span></span><br><span class="line">after 钩子没有 next 方法，不能改变导航</span><br><span class="line">router.afterEach(<span class="function"><span class="params">route</span> =&gt;</span> &#123; <span class="comment">// ...&#125;)</span></span><br></pre></td></tr></table></figure>
<h2 id="三、单个路由钩子"><a href="#三、单个路由钩子" class="headerlink" title="三、单个路由钩子"></a>三、单个路由钩子</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">        path: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        component: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        meta: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        beforeEnter: <span class="function">(<span class="params">to, <span class="keyword">from</span>, next</span>) =&gt;</span> &#123;     </span><br><span class="line">            <span class="comment">//进入时执行代码块</span></span><br><span class="line">        &#125;,</span><br><span class="line">        beforeLeave: <span class="function">(<span class="params">to, <span class="keyword">from</span>, next</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="comment">//离开时执行代码块</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="四、组件内的钩子"><a href="#四、组件内的钩子" class="headerlink" title="四、组件内的钩子"></a>四、组件内的钩子</h2><p>主要包括 beforeRouteEnter和beforeRouteUpdate ,beforeRouteLeave,这几个钩子都是写在组件里面也可以传三个参数(to,from,next)</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">Vue.component(<span class="string">&#x27;LifeCircle&#x27;</span>,&#123;</span><br><span class="line">    template: <span class="string">&#x27;#life-circle&#x27;</span>,</span><br><span class="line">    data () &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 初始化阶段钩子函数</span></span><br><span class="line">    beforeCreate () &#123; <span class="comment">//表示组件创建前的准备工作（ 初始化事件和生命周期 ）     </span></span><br><span class="line">      <span class="comment">//组件未创建，数据拿不到</span></span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">&#x27;1&#x27;</span>);</span><br><span class="line">    &#125;,</span><br><span class="line">    created () &#123; <span class="comment">// 组件创建结束</span></span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">&#x27;2&#x27;</span>)</span><br><span class="line">    &#125;,</span><br><span class="line">    beforeMount () &#123; </span><br><span class="line">      <span class="built_in">console</span>.log( <span class="string">&#x27;3&#x27;</span> )</span><br><span class="line">    &#125;,</span><br><span class="line">    mounted () &#123;     <span class="comment">//表示组件装载结束</span></span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">&#x27;4&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/wow/tags/GIT/" rel="tag"># -GIT</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/wow/2021/01/27/zy/vue%E6%8B%A6%E6%88%AA%E5%99%A8/" rel="prev" title="Hello '朵朵'">
                  <i class="fa fa-chevron-left"></i> Hello '朵朵'
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/wow/2021/01/29/zy/vue%E7%88%B6%E5%AD%90%E7%BB%84%E4%BB%B6%20%E5%AD%90%E7%88%B6%E7%BB%84%E4%BB%B6/" rel="next" title="Hello">
                  Hello <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">X y</span>
</div>
  <div class="powered-by">تطبيق الموقع <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/wow/js/utils.js"></script><script src="/wow/js/motion.js"></script><script src="/wow/js/schemes/muse.js"></script><script src="/wow/js/next-boot.js"></script>

  






  





</body>
</html>
